package com.wzdigit.wms.wms.service.core.asn;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzdigit.wms.basic.domain.asn.AsnHeader;
import com.wzdigit.wms.basic.enums.AsnHeaderStatusEnum;
import com.wzdigit.wms.basic.enums.AsnTypeEnum;
import com.wzdigit.wms.basic.mapper.core.order.AsnHeaderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 收货通知单信息 服务类
 * </p>
 *
 * @author
 * @since 2021-01-12
 */
@Service
@Slf4j
public class AsnHeaderService {
	
    @Autowired
    private AsnHeaderMapper asnHeaderMapper;

    public AsnHeaderMapper getMapper() {
        return asnHeaderMapper;
    }
    
    /**
     * 根据入参实体新增一条记录
     * @param asnHeader
     * @return
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public AsnHeader addNewOne(AsnHeader asnHeader) {
        int row = asnHeaderMapper.insert(asnHeader);
        log.info("成功新增{}条记录", row);
        return asnHeader;
    }

    /**
     * 根据传入的值更新
     * @param asnHeader
     * @return
     */
    public AsnHeader updateOne(AsnHeader asnHeader) {
        int row = asnHeaderMapper.updateById(asnHeader);
        log.info("成功更新{}条记录", row);
        return asnHeader;
    }

	public List<AsnHeader> selectProgressOrder() {
		QueryWrapper<AsnHeader> queryWrapper = new QueryWrapper<>();
		List<String> listAsnType = new ArrayList<>();
		listAsnType.add(AsnTypeEnum.R01.getCode());
		queryWrapper.in("ASN_TYPE", listAsnType);
		queryWrapper.ne("STATUS", AsnHeaderStatusEnum.FINISH.code);
		return this.asnHeaderMapper.selectList(queryWrapper);
	}

    public AsnHeader selectByNo(String billNo) {
        QueryWrapper<AsnHeader> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ASN_NO", billNo).or().eq("SOURCE_NO", billNo);
        List<AsnHeader> list = this.asnHeaderMapper.selectList(queryWrapper);
        if (list.size() == 0)
            return null;
        return list.get(0);
    }
}